package xin.marcher.module.seller.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.common.utils.ParamCheckUtil;
import xin.marcher.module.seller.api.SellerAbilityApi;
import xin.marcher.module.seller.domain.request.SellerGroupRequest;
import xin.marcher.module.seller.domain.request.SellerInfoRequest;
import xin.marcher.module.seller.domain.response.SellerGroupResponse;
import xin.marcher.module.seller.domain.response.SellerInfoResponse;
import xin.marcher.module.seller.service.SellerGroupService;
import xin.marcher.module.seller.service.SellerInfoService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 卖家能力API实现
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = SellerAbilityApi.class, retries = 0)
public class SellerAbilityApiImpl implements SellerAbilityApi {

    @Resource
    private SellerInfoService sellerInfoService;

    @Resource
    private SellerGroupService sellerGroupService;

    /**
     * 分页查询卖家组
     * <p>
     *
     * @param sellerGroupRequest
     * @return xin.marcher.module.common.core.JsonResult<xin.marcher.module.common.core.PageResult < SellerGroupResponse>>
     */
    @Override
    public JsonResult<PageResult<SellerGroupResponse>> getSellerGroupList(SellerGroupRequest sellerGroupRequest) {
        try {
            ParamCheckUtil.checkObjectNonNull(sellerGroupRequest);
            // if(CollectionUtils.isEmpty(sellerGroupRequest.getSellerGroupIdList())) {
            //     return JsonResult.buildError("卖家组ID不能为空");
            // }

            if (Objects.isNull(sellerGroupRequest.getSellerGroupType())) {
                return JsonResult.buildError("卖家组类型不能为空");
            }

            PageResult<SellerGroupResponse> sellerGroupResponseList = sellerGroupService.querySellerGroupInfo(sellerGroupRequest);
            return JsonResult.buildSuccess(sellerGroupResponseList);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(sellerGroupRequest), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(sellerGroupRequest), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据卖家ID和卖家类型获取卖家信息
     *
     * @param sellerInfoRequest
     * @return xin.marcher.module.common.core.JsonResult<java.util.List < SellerInfoResponse>>
     */
    @Override
    public JsonResult<List<SellerInfoResponse>> getSellerInfo(SellerInfoRequest sellerInfoRequest) {
        try {
            List<SellerInfoResponse> sellerInfoResponseList = sellerInfoService.querySellerInfoForRPC(sellerInfoRequest);
            return JsonResult.buildSuccess(sellerInfoResponseList);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(sellerInfoRequest), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(sellerInfoRequest), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询出所有的子卖家的sellerId
     *
     * @param sellerInfoRequest
     * @return
     */
    @Override
    public JsonResult<List<Long>> getChildSellerIdList(SellerInfoRequest sellerInfoRequest) {
        try {
            List<Long> sellerInfoResponseList = sellerInfoService.getChildSellerIdList(sellerInfoRequest);
            return JsonResult.buildSuccess(sellerInfoResponseList);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(sellerInfoRequest), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(sellerInfoRequest), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
